package com.chance.cc.crawler.development.command.job.domain.jingdong;

import com.alibaba.fastjson.JSON;
import com.chance.cc.crawler.core.CrawlerEnum;
import com.chance.cc.crawler.core.CrawlerJob;
import com.chance.cc.crawler.core.downloader.HttpConstant;
import com.chance.cc.crawler.core.downloader.HttpPage;
import com.chance.cc.crawler.core.downloader.HttpRequestBody;
import com.chance.cc.crawler.core.downloader.proxy.Proxy;
import com.chance.cc.crawler.core.record.CrawlerRequestRecord;
import com.chance.cc.crawler.development.command.job.CrawlerJobCommand;
import com.chance.cc.crawler.meta.core.bean.CrawlerMetaConstant;
import com.chance.cc.crawler.meta.core.bean.job.CrawlerScheduleJob;
import org.apache.commons.lang3.StringUtils;

import static com.chance.cc.crawler.core.CrawlerEnum.CrawlerRequestType.*;

/**
 * created by CC on 2020/11/26
 * mail 279020185@qq.com
 */
public class JDH5DetailCrawlerScheduleJob {

    public static String domainId = "jingdong";

    private static String service_ip = "192.168.1.215";
    private static int service_port = 9599;

    private static Proxy proxy = new Proxy();
    static {
        //代理配置
        proxy.setHost("http-dyn.abuyun.com");
        proxy.setPort(9020);
        proxy.setUsername("HL89Q19E86E2987D");
        proxy.setPassword("71F33D94CE5F7BF2");
    }

    public static void main(String[] args) {
        publishJDDetailCrawlerScheduleJobInfo();
    }

    public static CrawlerJob publishJDDetailCrawlerScheduleJobInfo(){


        CrawlerJob jdCrawlerJob = jdCrawlerJob(domainId);

        //发布定时采集作业
        //发布定时采集作业
        CrawlerJobCommand crawlerJobCommand = new CrawlerJobCommand(service_ip,service_port);
        CrawlerScheduleJob crawlerScheduleJob = new CrawlerScheduleJob();
        crawlerScheduleJob.setDomain(domainId);
        crawlerScheduleJob.setCrawlerJob(JSON.toJSONString(jdCrawlerJob));
        crawlerScheduleJob.setJobType(CrawlerMetaConstant.ScheduleCrawlerJobType.crawler.enumVal());
        crawlerScheduleJob.setNote("京东商品详情定时采集");
        crawlerScheduleJob.setCrawlerKey(jdCrawlerJob.generateCrawlerKey());
        HttpPage page = crawlerJobCommand.addOrUpdateCrawlerScheduleJob(crawlerScheduleJob);
        System.out.println("发布作业：" + page.getRawText());
        return jdCrawlerJob;

    }

    /**
     * taobao采集
     * @param domainId
     * @return
     */
    public static CrawlerJob jdCrawlerJob(String domainId) {

        CrawlerRequestRecord requestRecord = CrawlerRequestRecord.builder()
                .startPageRequest(domainId, turnPageItem)
                .httpUrl("https://item.m.jd.com/product/")
                .releaseTime(System.currentTimeMillis())
                .needWashed(false)
                .needParsed(false)
                .resultLabelTag(CrawlerEnum.CrawlerDataType.article)
                .proxy(proxy)
                .build();
        requestRecord.setDownload(false);
        requestRecord.setSkipPipeline(true);

        //初始 comment crawler request record
        String requestQueueName = StringUtils.joinWith("-","crawler",domainId,"article","queue");
        CrawlerRequestRecord initArticleCrawlerRecord = CrawlerRequestRecord.builder()
                .startPageRequest("jingdong_article_keywords",turnPageItem)
                .httpUrl("http://192.168.1.215:9599/crawler/domain/common/api/v1/jingdong/results/queue/init?requestQueueName="+requestQueueName)
                .requestLabelTag(supportSource)
                .requestLabelTag(internalDownload)
                .build();
        HttpRequestBody jsonBody = HttpRequestBody.json(JSON.toJSONString(requestRecord), "utf-8");
        initArticleCrawlerRecord.getHttpRequest().setMethod(HttpConstant.Method.POST);
        initArticleCrawlerRecord.getHttpRequest().setRequestBody(jsonBody);

        //user agents source record
        CrawlerRequestRecord userAgentsCrawlerRecord = CrawlerRequestRecord.builder()
                .startPageRequest("jingdong_article_user_agents", turnPageItem)
                .httpUrl("https://fake-useragent.herokuapp.com/browsers/0.1.11")
                .requestLabelTag(supportSource)
                .requestLabelTag(internalDownload)
                .build();

        return CrawlerJob.builder()
                .crawlerJobThreadNumber(20)
                .triggerInfo(
                        domainId,
                        CrawlerMetaConstant.ScheduleJobTrigger_Cron,
                        System.currentTimeMillis(),
                        StringUtils.joinWith("-","article",CrawlerMetaConstant.ScheduleJobTriggerJob_Realtime))
                .crawlerRequestQueue(CrawlerMetaConstant.redisRequestQueue(requestQueueName))
                .fileResultPipeline(null, "/data/chance_crawler_test/data/jingdong/jingdong_article.log", false)
                .kafkaResultPipeline(null,"tmp_e_commerce",null)
                .requestRecord(requestRecord)
                .supportRecord(userAgentsCrawlerRecord)
                .supportRecord(initArticleCrawlerRecord)
                .build();
    }

}
